<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>完美拖拽</title>
	<style>
		*{
			margin:0;
			padding:0;
		}
		#big{
			position: absolute;
			left:0;
			top:0;
			width: 200px;
			height: 200px;
			background: cyan;
		}
		button{
			float: right;
		}
	</style>
</head>
<body>
	<div id="big">
		<button>返回</button>
		<span></span>
	</div>
</body>
<script>
	var oBig = document.querySelector('#big');
	var oBtn = document.querySelector('button');
	var oSp = document.querySelector('span');
	var arr = [[0,0]];
	oBig.onmousedown = function(e){
		var e = event||window.event;
		var le = e.offsetX;
		var to = e.offsetY;
		document.onmousemove = function(evt){
			var evt = event||window.event;
			var clX = evt.clientX - le;
			var clY = evt.clientY - to;
			if(clY<=0){
				clY = 0;
			}else if(clY+oBig.offsetHeight>=window.innerHeight){
				clY = window.innerHeight - oBig.offsetHeight;
			}
			if(clX<=0){
				clX = 0;
			}else if(clX+oBig.offsetWidth>=window.innerWidth){
				clX = window.innerWidth - oBig.offsetWidth;
			}
			oBig.style.left = clX + 'px';
			oBig.style.top  = clY + 'px';
			oSp.innerHTML = clX + ':' + clY;
			arr.push({'left':clX,'lefts':clY})
		}	
	}
	oBig.onmouseup = function(){
		document.onmousemove = null;
	}
	oBtn.onclick = function(){
		var c = arr.length-1;
		var timer;
		clearInterval(timer);
		timer = setInterval(function(){
			console.log(oBig.style.left,oBig.style.top)
			oBig.style.left = arr[c].left + 'px';
			oBig.style.top  = arr[c].lefts + 'px';
			oSp.innerHTML = oBig.style.left + ':' + oBig.style.top;
			c-=1;
			if(c==0){
				oBig.style.left = 0 + 'px';
				oBig.style.top  = 0 + 'px';
				oSp.innerHTML = oBig.style.left + ':' + oBig.style.top;
				clearInterval(timer);
				arr  = [{left:0,top:0}];
			}
		},10)
	}
</script>
</html>